package cn.jdbc;

import com.sun.xml.internal.bind.v2.model.core.ID;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class TestJdbc {
    public static void main(String[] args) {
        method();
        method1();
    }
    private static void method1() {
        Connection con=null;
        Statement state=null;
        int result ;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
             con= DriverManager.getConnection("jdbc:mysql://:3307/jt_db", "root", "root");
            state = con.createStatement();
            result = state.executeUpdate("update acc set money=998.88 where id=1");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                state.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    private static void method() {
        Connection con=null;
        Statement state=null;
        ResultSet result =null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://:3307/jt_db?characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "root", "root");
            state = con.createStatement();
            result = state.executeQuery("select * from acc");
            Map map=new HashMap();
            int a=1;
            while (result.next()){
                int id=result.getInt("id");
                String name=result.getString("name");
                Double money=result.getDouble("money");
                map.put("第"+a+"条数据", "id="+id+" name="+name+" money="+money);
                a++;
            }
            System.out.println(map.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                result.close();
                state.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }

}
